﻿using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Excel;
using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.AuthorizationUserManager;
using NP.BPMReportPlatform.AuthorizationUserManager.Dtos;
using NP.BPMReportPlatform.Entities.BPM_Business.Reports;
using NP.BPMReportPlatform.HR.Dto;
using NP.BPMReportPlatform.Reports.OrderMaterialDetail.Dtos;
using NPOI.HSSF.Record;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.Reports.OrderMaterialDetail
{
    [Authorize]
    [ApiController]
    [Route("api/Reports/OrderMaterialDetail")]
    public class OrderMaterialDetailController : BPMReportPlatformController
    {
        private readonly IOrderMaterialDetailService _detailService;
        private readonly IExcelImporter _importer;
        public OrderMaterialDetailController(
            IOrderMaterialDetailService detailService,
            IExcelImporter importer)
        {
            _detailService = detailService;
            _importer = importer;
        }

        /// <summary>
        /// 分页获取订单明细
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetList")]
        public async Task<PagedResultDto<OrderMaterialDetailDto>> GetUserList([FromBody] OrderMaterialDetailInput input)
        {
            return await _detailService.GetList(input);
        }

        /// <summary>
        /// 导出订单明细
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ExportExcel")]
        public async Task<XlsxFileResult<OrderMaterialDetailDto>> ExportExcel([FromBody] OrderMaterialDetailInput input)
        {
            var rst = await _detailService.ExportExcel(input);
            return new XlsxFileResult<OrderMaterialDetailDto>(rst, "导出Excel");
        }

        /// <summary>
        /// 导入订单明细
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ImportExcel")]
        public async Task<ImportResultExt<OrderMaterialDetailDto>> ImportExcel(IFormFile file)
        {
            if (file == null)
            {
                var res = new ImportResultExt<OrderMaterialDetailDto>
                {
                    Exception = new Exception("请上传文件！")
                };
                return res;
            }
            var stream = file.OpenReadStream();
            var rst = await _importer.Import<OrderMaterialDetailDto>(stream);
            stream.Close();
            if (rst.HasError)
            {
                var resp = new ImportResultExt<OrderMaterialDetailDto>()
                {
                    Exception = rst.Exception,
                    RowErrors = rst.RowErrors,
                    TemplateErrors = rst.TemplateErrors
                };
                return resp;
            }
            else
            {
                return await _detailService.ImportExcel(rst.Data.ToList());
            }
        }

        /// <summary>
        /// 导出订单明细导入模板
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("ExportTemplate")]
        public async Task<FileResult> ExportTemplate()
        {
            var rst = await _detailService.ExportTemplate();
            return File(rst, "application/octet-stream", "订单明细导入模板.xlsx");
        }

        /// <summary>
        /// 导出不足吨位数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("ExportTonUnEnoughData")]
        public async Task<FileResult> ExportTonUnEnoughData()
        {
            var rst = await _detailService.ExportTonUnEnoughData();
            rst.Close();
            return File(rst.ToArray(), "application/octet-stream", "订单明细吨位不足数据导出.xlsx");
        }

        /// <summary>
        /// 导入成品计划报表
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ImportEndProductPlanData")]
        public async Task<ImportResultExt<EndProductPlanDto>> ImportEndProductPlanData(IFormFile file)
        {
            if (file == null)
            {
                var res = new ImportResultExt<EndProductPlanDto>
                {
                    Exception = new Exception("请上传文件！")
                };
                return res;
            }
            var stream = file.OpenReadStream();
            var rst = await _importer.Import<EndProductPlanDto>(stream);
            stream.Close();
            if (rst.HasError)
            {
                var resp = new ImportResultExt<EndProductPlanDto>()
                {
                    Exception = rst.Exception,
                    RowErrors = rst.RowErrors,
                    TemplateErrors = rst.TemplateErrors
                };
                return resp;
            }
            else
            {
                return await _detailService.ImportEndProductPlanData(rst.Data.ToList());
            }
        }

        /// <summary>
        /// 导入物料基础配置
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ImportConfig")]
        public async Task<ImportResultExt<OrderMaterialConfigDto>> ImportConfig(IFormFile file)
        {
            if (file == null)
            {
                var res = new ImportResultExt<OrderMaterialConfigDto>
                {
                    Exception = new Exception("请上传文件！")
                };
                return res;
            }
            var stream = file.OpenReadStream();
            var rst = await _importer.Import<OrderMaterialConfigDto>(stream);
            stream.Close();
            if (rst.HasError)
            {
                var resp = new ImportResultExt<OrderMaterialConfigDto>()
                {
                    Exception = rst.Exception,
                    RowErrors = rst.RowErrors,
                    TemplateErrors = rst.TemplateErrors
                };
                return resp;
            }
            else
            {
                return await _detailService.ImportConfig(rst.Data.ToList());
            }
        }

        /// <summary>
        /// 导出未清分析汇总数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("ExportUnclearSummaryData")]
        public async Task<FileResult> ExportUnclearSummaryData()
        {
            var rst = await _detailService.ExportUnclearSummaryData();
            rst.Close();
            return File(rst.ToArray(), "application/octet-stream", "未清分析汇总数据导出.xlsx");
        }

        /// <summary>
        /// 导入可用库存
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ImportAvailableInventoryData")]
        public async Task<ImportResultExt<AvailableInventoryDto>> ImportAvailableInventoryData(IFormFile file)
        {
            if (file == null)
            {
                var res = new ImportResultExt<AvailableInventoryDto>
                {
                    Exception = new Exception("请上传文件！")
                };
                return res;
            }
            var stream = file.OpenReadStream();
            var rst = await _importer.Import<AvailableInventoryDto>(stream);
            stream.Close();
            if (rst.HasError)
            {
                var resp = new ImportResultExt<AvailableInventoryDto>()
                {
                    Exception = rst.Exception,
                    RowErrors = rst.RowErrors,
                    TemplateErrors = rst.TemplateErrors
                };
                return resp;
            }
            else
            {
                return await _detailService.ImportAvailableInventoryData(rst.Data.ToList());
            }
        }
    }
}
